package com.yesep.license.swing;

import com.yesep.license.MyRSAEnCoderTools;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;


public class MainCrytoFrame {

	final static int frameWidth = 500;
	final static int frameHeight = 450;
	
	public static void main(String[] args) throws IOException{
		
		Font buttonFont = new Font("宋体",Font.PLAIN,14);
		Font textAreaFont = new Font("宋体",Font.PLAIN,13);
		UIManager.put("Button.font", buttonFont); 
		UIManager.put("TextArea.font", textAreaFont); 
		
		JFrame mainframe = new JFrame("自助打印证书生成程序");  
		// 设置高度和宽度 
		mainframe.setSize(frameWidth, frameHeight);
		mainframe.setResizable(false);
		mainframe.setLocationByPlatform(true);
		JLabel label = new JLabel("<html><div style='color:red;'>说明:</div>" +
				"<div style='font-weight:normal;'>证书路径"+ MyRSAEnCoderTools.licenseFilePath +"</div></html>");
		
		label.setSize(new Dimension(frameWidth, 20));
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new FlowLayout(30));
		topPanel.add(label);
		mainframe.add(topPanel,BorderLayout.NORTH); 
        final JTextArea jtextArea = new ConsoleTextArea();
        jtextArea.setRows(10);
        jtextArea.setColumns(5);
        jtextArea.setAutoscrolls(true); 
        jtextArea.setEditable(false);
        mainframe.add(new JScrollPane(jtextArea),BorderLayout.CENTER); 
        
		final JButton jbuttonInit = new JButton("初始化路径");
		final JButton jbuttonGenrate = new JButton("生成证书");
		final JButton jbuttonClear = new JButton("清空控制台");
		jbuttonInit.setToolTipText("初始化生成License的文件路径，请不要连续点击");
		jbuttonGenrate.setToolTipText("生成ZZFWServer.lic文件，请不要连续点击");
		jbuttonClear.setToolTipText("清空控制台");
		
		jbuttonInit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == jbuttonInit) {
					MyRSAEnCoderTools.initLicensePath();
		        }
			}
		});
		jbuttonGenrate.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == jbuttonGenrate) {
					MyRSAEnCoderTools.genLicenseFile();
		        }
			}
		});
		
		jbuttonClear.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == jbuttonClear) {
					jtextArea.setText(null);
		        }
			}
		});
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout(30));
		bottomPanel.add(jbuttonInit);
		bottomPanel.add(jbuttonGenrate);
		bottomPanel.add(jbuttonClear);
		
		mainframe.add(bottomPanel, BorderLayout.SOUTH);
		//mainframe.pack();  
		mainframe.setVisible(true);  
		mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
		
	}
	
	
	
}
